﻿using System;
using System.Collections.Generic;
using Educational.Model.Entity;
using NHibernate;
using NHibernate.Criterion;

namespace Educational.Model.EntityManager
{
    public class TeacherManager
    {
        public static void Add(ISession session, Teacher an)
        {
            session.Save(an);
        }

        public static void Delete(ISession session, Teacher an)
        {
            session.Delete(an);
        }

        public static void Update(ISession session, Teacher an)
        {
            session.Update(an);
        }

        public static Teacher Get(ISession session, int itemId)
        {
            return session.Get<Teacher>(itemId);
        }

        public static Teacher Get1(ISession session, string loginId)
        {
            ICriteria c = session.CreateCriteria(typeof(Teacher));
            c.Add(Expression.Eq("loginId", loginId));
            return c.UniqueResult<Teacher>();
        }

        public static IList<Teacher> Gets1(ISession session, string teacherName, string loginId, int schoolId, int rowIndex, int rowCount)
        {
            ICriteria c = session.CreateCriteria(typeof(Teacher));
            if (!String.IsNullOrEmpty(teacherName))
                c.Add(Expression.Like("teacherName", "%" + teacherName + "%"));
            if (!String.IsNullOrEmpty(loginId))
                c.Add(Expression.Eq("loginId", loginId));
            c.Add(Expression.Not(Expression.Eq("status", 8)));
            c.SetFetchMode("jobTitle", FetchMode.Eager);
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            if (schoolId > 0)
                c1.Add(Expression.Eq("schoolId", schoolId));
            c.SetFirstResult(rowIndex);
            c.SetMaxResults(rowCount);
            c.AddOrder(Order.Desc("teacherId"));
            return c.List<Teacher>();
        }

        public static int Gets1Count(ISession session, string teacherName, string loginId, int schoolId)
        {
            ICriteria c = session.CreateCriteria(typeof(Teacher));
            if (!String.IsNullOrEmpty(teacherName))
                c.Add(Expression.Like("teacherName", "%" + teacherName + "%"));
            if (!String.IsNullOrEmpty(loginId))
                c.Add(Expression.Eq("loginId", loginId));
            c.Add(Expression.Not(Expression.Eq("status", 8)));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            if (schoolId > 0)
                c1.Add(Expression.Eq("schoolId", schoolId));
            return (int)c.SetProjection(Projections.Count("teacherId")).UniqueResult();
        }

        public static IList<Teacher> Gets2(ISession session, int schoolId, int[] teacherIds)
        {
            ICriteria c = session.CreateCriteria(typeof(Teacher));
            c.Add(Expression.In("teacherId", teacherIds));
            c.SetFetchMode("jobTitle", FetchMode.Select);
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            if (schoolId > 0)
                c1.Add(Expression.Eq("schoolId", schoolId));
            return c.List<Teacher>();
        }

        public static IList<Teacher> Gets3(ISession session, string teacherName, string loginId, int schoolId, int rowIndex, int rowCount)
        {
            ICriteria c = session.CreateCriteria(typeof(Teacher));
            if (!String.IsNullOrEmpty(teacherName))
                c.Add(Expression.Like("teacherName", "%" + teacherName + "%"));
            if (!String.IsNullOrEmpty(loginId))
                c.Add(Expression.Eq("loginId", loginId));
            c.Add(Expression.Eq("status", 9));
            c.SetFetchMode("jobTitle", FetchMode.Eager);
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            if (schoolId > 0)
                c1.Add(Expression.Eq("schoolId", schoolId));
            c.SetFirstResult(rowIndex);
            c.SetMaxResults(rowCount);
            c.AddOrder(Order.Desc("teacherId"));
            return c.List<Teacher>();
        }

        public static int Gets3Count(ISession session, string teacherName, string loginId, int schoolId)
        {
            ICriteria c = session.CreateCriteria(typeof(Teacher));
            if (!String.IsNullOrEmpty(teacherName))
                c.Add(Expression.Like("teacherName", "%" + teacherName + "%"));
            if (!String.IsNullOrEmpty(loginId))
                c.Add(Expression.Eq("loginId", loginId));
            c.Add(Expression.Eq("status", 9));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            if (schoolId > 0)
                c1.Add(Expression.Eq("schoolId", schoolId));
            return (int)c.SetProjection(Projections.Count("teacherId")).UniqueResult();
        }
    }
}
